using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Company
    {
          #region Common
        public eCompany Data;

        public Company()
        {
            Data = new eCompany();
		}

        ~Company()
        {
		}

		public virtual void Dispose()
        {
		}

        public int DeleteById(int ID)
        {
            int nResult = 0;
            cCompany objDAL = new cCompany();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public void LoadById(int ID)
        {
            cCompany objDAL = new cCompany();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }

        public int Insert()
        {
            int nResult = 0;
            cCompany objInfor = new cCompany(); 
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.CompanyID ;
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cCompany objInfor = new cCompany();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID; 
            }
            catch
            {
                nResult = 0; 
            }
            return nResult;
        }

        

        private void MappingData(cCompany objInfor)
        {
            try { objInfor.CompanyName1 = Data.CompanyName1; }
            catch { objInfor.CompanyName1 = ""; } 
            try { objInfor.CompanyName2 = Data.CompanyName2;}
            catch {objInfor.CompanyName2 =""; } 
            try { objInfor.CompanyName3 = Data.CompanyName3;}
            catch { objInfor.CompanyName3 = ""; } 
            try { objInfor.Address1 = Data.Address1;}
            catch { objInfor.Address1 = ""; } 
            try { objInfor.Address2 = Data.Address2;}
            catch { objInfor.Address2 = ""; } 
            try { objInfor.Address3 = Data.Address3;}
            catch { objInfor.Address3 = ""; } 
            try {  objInfor.Hotline = Data.Hotline;}
            catch { objInfor.Hotline = ""; } 
            try {   objInfor.Tel = Data.Tel;}
            catch { objInfor.Tel = ""; } 
            try { objInfor.Fax = Data.Fax;}
            catch { objInfor.Fax = ""; } 
            try {   objInfor.Email = Data.Email;}
            catch { objInfor.Email = ""; } 
            try {    objInfor.Website = Data.Website;}
            catch { objInfor.Website = ""; } 
            try { objInfor.Copyright = Data.Copyright; }
            catch { objInfor.Copyright = ""; }

            try { objInfor.YahooAccount1 = Data.YahooAccount1; }
            catch { objInfor.YahooAccount1 = ""; }

            try { objInfor.YahooAccount2 = Data.YahooAccount2; }
            catch { objInfor.YahooAccount2 = ""; }

            try { objInfor.YahooAccount3 = Data.YahooAccount3; }
            catch { objInfor.YahooAccount3 = ""; }

            try { objInfor.YahooAccount4 = Data.YahooAccount4; }
            catch { objInfor.YahooAccount4 = ""; }

            try { objInfor.SkypeAccount1 = Data.SkypeAccount1; }
            catch { objInfor.SkypeAccount1 = ""; }

            try { objInfor.SkypeAccount2 = Data.SkypeAccount2; }
            catch { objInfor.SkypeAccount2 = ""; }

            try { objInfor.SkypeAccount3 = Data.SkypeAccount3; }
            catch { objInfor.SkypeAccount3 = ""; }

            try { objInfor.SkypeAccount4 = Data.SkypeAccount4; }
            catch { objInfor.SkypeAccount4 = ""; } 

             try { objInfor.BranchAddress1 = Data.BranchAddress1; }
            catch { objInfor.BranchAddress1 = ""; } 

             try { objInfor.BranchAddress2 = Data.BranchAddress2; }
            catch { objInfor.BranchAddress2 = ""; } 

             try { objInfor.BranchAddress3 = Data.BranchAddress3; }
            catch { objInfor.BranchAddress3 = ""; } 

             try { objInfor.BranchHotline = Data.BranchHotline; }
            catch { objInfor.BranchHotline = ""; } 

             try { objInfor.BranchTel = Data.BranchTel; }
            catch { objInfor.BranchTel = ""; } 

             try { objInfor.BranchFax = Data.BranchFax; }
            catch { objInfor.BranchFax = ""; }

             try { objInfor.BranchEmail = Data.BranchEmail; }
             catch { objInfor.BranchEmail = ""; }

             try { objInfor.BranchCompanyName1 = Data.BranchCompanyName1; }
             catch { objInfor.BranchCompanyName1 = ""; }

             try { objInfor.BranchCompanyName2 = Data.BranchCompanyName2; }
             catch { objInfor.BranchCompanyName2 = ""; }

             try { objInfor.BranchCompanyName3 = Data.BranchCompanyName3; }
             catch { objInfor.BranchCompanyName3 = ""; } 
      
        }

        private void ConvertData(cCompany objInfor)
        {
            Data.CompanyName1=objInfor.CompanyName1  ;
            Data.CompanyName2=objInfor.CompanyName2  ;
            Data.CompanyName3 =objInfor.CompanyName3  ;
            Data.Address1  =objInfor.Address1  ;
            Data.Address2   =objInfor.Address2  ;
            Data.Address3  =objInfor.Address3  ;
            Data.Hotline  =objInfor.Hotline ;
            Data.Tel  =objInfor.Tel  ;
            Data.Fax  =objInfor.Fax  ;
            Data.Email =objInfor.Email  ;
            Data.Website =objInfor.Website  ;
            Data.Copyright =objInfor.Copyright  ;
            Data.YahooAccount1 = objInfor.YahooAccount1;
            Data.YahooAccount2 = objInfor.YahooAccount2;
            Data.YahooAccount3 = objInfor.YahooAccount3;
            Data.YahooAccount4 = objInfor.YahooAccount4;
            Data.SkypeAccount1 = objInfor.SkypeAccount1;
            Data.SkypeAccount2 = objInfor.SkypeAccount2;
            Data.SkypeAccount3 = objInfor.SkypeAccount3;
            Data.SkypeAccount4 = objInfor.SkypeAccount4;


            Data.BranchAddress1 = objInfor.BranchAddress1;
            Data.BranchAddress2 = objInfor.BranchAddress2;
            Data.BranchAddress3 = objInfor.BranchAddress3;
            Data.BranchHotline = objInfor.BranchHotline;
            Data.BranchTel = objInfor.BranchTel;
            Data.BranchFax = objInfor.BranchFax;
            Data.BranchEmail = objInfor.BranchEmail;
            Data.BranchCompanyName1 = objInfor.BranchCompanyName1;
            Data.BranchCompanyName2 = objInfor.BranchCompanyName2;
            Data.BranchCompanyName3 = objInfor.BranchCompanyName3;
        }

        public DataTable Search()
        {
            DataTable tbResult = new DataTable();
            cCompany objDAL = new cCompany();
            tbResult = objDAL.Search();
            return tbResult;
        }
      
        #endregion
    }
}
